package elcs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import elcs.board.model.ListVO;
import elcs.member.model.MemberVO;
import elcs.mentoring.model.MentoringService;
import elcs.mentoring.model.MentoringVO;
import elcs.mentoring.model.NoteVO;

@Controller
public class MentoringController {
	@Resource(name="mentoringServiceImpl")
	private MentoringService service;

	
	@RequestMapping("mentoring/mentoring.do")
	public String mentoring(){
		return "mentoring.mentoring";
	}
	
	@RequestMapping("mentoring/status.do")
	public ModelAndView statusView(HttpSession session) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		Map qMap=new HashMap();
		qMap.put("id", mvo.getId());
		qMap.put("page", 1);
		qMap.put("status", "all");
		return new ModelAndView("status.mentoring","statusMap",service.statusView(qMap));
	}
	@RequestMapping("mentoring/statusAjax.do")
	@ResponseBody
	public ListVO statusViewAjax(HttpSession session,int page,String status) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		Map qMap=new HashMap();
		qMap.put("id", mvo.getId());
		qMap.put("page", page);
		qMap.put("status", status);
		return service.questionView(qMap);
	}

	/**
	 * Mentoring > Question > 멘토에게 질문하기 가능
	 * @SSA
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/mentoring/question.do")
	public ModelAndView questionView(HttpSession session) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		List llist=service.getMyMentorLevel(mvo.getLevel());

		return new ModelAndView("question.mentoring","llist",llist);
	}
	/**
	 * 멘토 레벨 선택 시  자신의 레벨과 비교하여 
	 * 소모되는 포인트를 계산하여 보여준다.
	 * @SSA
	 * @param level
	 * @param session
	 * @return
	 * @throws Exception
	 */ 
	@RequestMapping("/mentoring/getPointForRequest.do")
	@ResponseBody
	public int getPointForRequestAjax(int level, HttpSession session) throws Exception{
		Map map=new HashMap();
		map.put("ulevel", level);
		map.put("mylevel", ((MemberVO)session.getAttribute("mvo")).getLevel());
		return service.getPointForRequest(map);
	}
	/**
	 * 선택한 레벨의 해당하는 임의의 멘토에게 질문을 전송한다.
	 * 레벨 별 해당되는 포인트가 감소된다.
	 * BY SSA
	 * @throws Exception  
	 * 
	 */
	@RequestMapping("/mentoring/sendMentorQuestion.do")
	public String sendMentorQuestion(NoteVO nvo, HttpSession session) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		nvo.setSender(mvo);
		service.sendMentorQuestion(nvo);
		return "redirect:status.do";
	}
	
	@RequestMapping("/mentoring/rejectQuestion.do")
	public String rejectQuestionAjax(int no,int page,String status) throws Exception{
		service.rejectMentorQuestion(no);
		return "redirect:statusAjax.do?page="+page+"&status="+status;

	}
	
	@RequestMapping("/mentoring/viewContent.do")
	@ResponseBody
	public NoteVO viewContent(int no,HttpSession session) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		return service.getContent(no,mvo.getId());

	}
	   @RequestMapping("/mentoring/senderDeleteNote.do")
	   public String senderDeleteNote(int no,String uid) throws Exception{
		   System.out.println("보낸거삭제: ");
		   service.senderDeleteNote(no);
		   return "redirect:getNoteList.do?uid="+uid;
	   }
	   @RequestMapping("/mentoring/receiverDeleteNote.do")
	   public String receiverDeleteNote(int no,String uid) throws Exception{
		   System.out.println("받은거삭제: ");
		   service.receiverDeleteNote(no);
		   return "redirect:getNoteList.do?uid="+uid;
	   }
	   
	     
	   /**
	    * 답장 팝업창을 보여준다.
	    * by ssa
	 * @throws Exception 
	    *     
	    */
	   @RequestMapping("/mentoring/replyView.do")
		public ModelAndView replyView(int no) throws Exception
		{
			NoteVO nvo=service.viewNoteContent(no);
			return new ModelAndView("/WEB-INF/view/mentoring/mentoring_reply_popup","nvo",nvo);
		}
	   @RequestMapping("/mentoring/replyMentorQuestion.do")
	   public ModelAndView replyMentorQuestion(NoteVO nvo,HttpSession session) throws Exception{
		   service.replyMentorQuestion(nvo);
		   return new ModelAndView("redirect:status.do"); 
	   }
	   
	   @RequestMapping("/mentoring/deleteQuestion.do")
	   public String deleteQuestionAjax(NoteVO nvo,HttpSession session,int page,String status) throws Exception{
			System.out.println("엔브이오"+nvo);
		   MemberVO mvo=(MemberVO)session.getAttribute("mvo");
			service.deleteNote(nvo,mvo.getId());			
		   return "redirect:statusAjax.do?page="+page+"&status="+status;
	   }


	     
/**
 * 답변을 받은 후 멘토에게 멘토신청을 한다. ssa
 * @param no
 * @return
 * @throws Exception
 */
@RequestMapping("/mentoring/sendMentorRequest.do")
@ResponseBody
public List sendMentorRequest(int no,HttpSession session) throws Exception{
	   MemberVO mvo=(MemberVO)session.getAttribute("mvo");
	   System.out.println("sendMentorRequest: "+no);
	   return service.sendMentorRequest(no,mvo.getId());
	   
}
/**
 * 멘티가 멘토신청을 하면 수락 또는 거절을 할 수 있다.
 * @param mentoVO
 * @param session
 * @return
 * @throws Exception
 */
@RequestMapping("/mentoring/replyReceivedRequest.do")
@ResponseBody
public List replyReceivedRequestAjax(MentoringVO mentoVO) throws Exception{
	   System.out.println("replyMentorRequest: "+mentoVO);
	   return service.replyReceivedRequest(mentoVO);
	   
}

@RequestMapping("/mentoring/cancelRequest.do")
@ResponseBody
public List cancelRequestAjax(MentoringVO mentoVO) throws Exception{
	   System.out.println("replyMentorRequest: "+mentoVO);
	   return service.cancelRequest(mentoVO);
	   
}
/**
 * 나의 멘토와 멘티 리스트를 보여준다.
 * @throws Exception 
 * 
 */
@RequestMapping("/mentoring/myMentoring.do")  
public ModelAndView myMentoringView(HttpSession session) throws Exception{
	   MemberVO mvo=(MemberVO)session.getAttribute("mvo");
	   return new ModelAndView("mymentoring.mentoring","mentoringMap",service.myMentoringView(mvo.getId()));
}
@RequestMapping("/mentoring/getNoteList.do")
public ModelAndView getNoteList(String uid, HttpSession session) throws Exception{
	MemberVO mvo=(MemberVO) session.getAttribute("mvo");
	ModelAndView mv=new ModelAndView("note.mentoring");
	mv.addObject("uid",uid);
	Map map=new HashMap();
	mv.addObject("map",map);
	map.put("uid", uid);
	map.put("myid", mvo.getId());
	List nlist=service.getNoteList(map);
	mv.addObject("nlist",nlist);
	   return mv;
}

@RequestMapping("/mentoring/sendNote.do")
public String sendNote(NoteVO nvo,HttpSession session) throws Exception{
	   MemberVO mvo=(MemberVO) session.getAttribute("mvo");
	   nvo.setSender(mvo);
	   service.sendNote(nvo);
	   return "redirect:getNoteList.do?uid="+nvo.getReceiver().getId();
}

@RequestMapping("/mentoring/breakMentorRelationship.do")
public String breakMentorRelationship(MentoringVO mentoVO, HttpSession session) throws Exception{
	   MemberVO mvo=(MemberVO) session.getAttribute("mvo");
	   mentoVO.setMentee(mvo);
	service.breakMentorRelationship(mentoVO);
	return "redirect:myMentoring.do";
}
}
